import http from '@ohos.net.http'
import axios from '@ohos/axios'
import Article from '../viewModel/Article'

class ArticleModel {
  baseUrl: string = 'http://192.168.1.16:8088/v1/'
  page: number = 0

  getArticleList(): Promise<Article[]> {
    return new Promise((resolve, reject) => {
      let htpReq = http.createHttp()
      console.log(`article?page=${this.page}`)
      htpReq.request(
        this.baseUrl + `article?page=${this.page}`,
        {
          method: http.RequestMethod.GET
        }
      )
        .then(rsp => {
          if (rsp.responseCode === 200) {
            console.log("查询成功")
            resolve(JSON.parse(rsp.result.toString()))
          } else {
            reject("查询失败， " + JSON.stringify(rsp))
          }
        })
        .catch(rsp => {
          reject("查询失败， " + JSON.stringify(rsp))
        })
    })
  }

  getArticleListWithAxios(): Promise<Article[]> {

    return new Promise((resolve, reject) => {
      axios.get(this.baseUrl + "article", {
        params: {
          page: this.page,
        }
      })
        .then((rsp) => {
          if (rsp.status === 200) {
            resolve(rsp.data)
          } else {
            reject("查询失败")
          }
        })
        .catch(err => {
          console.log("test, get Error :", err)
          reject("查询失败")
        })
    })
  }
}

const articleModel = new ArticleModel()

export default articleModel as ArticleModel